import { Request, toast } from '../../../utils/util.js'
let request = new Request()
let app = getApp()
Page({
  data: {
    chatId : 0,
    chatType : 0,
    content : '',
    location : '',
    longitude : '',
    latitude : '',
    reward : false,
    images : [],
    
    goods_name : '',
    goods_no: '',
    sale_price: '',
    agent_price: '',
    category_id: 0,
    category_name : '',
    add: true,

  },

  input (e) {
    let target = e.currentTarget.dataset.target
    let update = {}
    update[target] = e.detail.value
    this.setData(update)
  },

  addImage () {
    let images = this.data.images
    let d = new Date()
    images.push({file : '', id : d.getTime()})
    this.setData({ images: images})
  },

  uploadPic(e) {
    let d = new Date()
    let file = e.detail.value
    let index = parseInt(e.target.dataset.index)
    let images = JSON.parse(JSON.stringify(this.data.images))
    images.push({ file: file, id: d.getTime()})

    this.setData({ images: images})
    
    /**let update = {}
    update[`images[${index}].file`] = file
    if (index < 8) {
      let d = new Date()
      update[`images[${index + 1}]`] = { file: '', id: d.getTime() }
    }
    this.setData(update)*/
  },

  clearPic(e) {
    let index = parseInt(e.target.dataset.index)
    let images = this.data.images
    images.splice(index, 1)

    this.setData({ images: images})
  },

  selectPostion () {
    wx.chooseLocation({
      success : res => {
        this.setData({ location: res.name, longitude: res.longitude, latitude : res.latitude})
      }
    })
  },

  clearLocation () {
    this.setData({ location: '', longitude: '', latitude: '' })
  },

  toSelectCategory () {
    wx.navigateTo({
      url: '/pages/goodsCategory/index'
    })
  },

  clearCategory () {
    this.setData({category_id : 0, category_name : ''})
  },

  submit () {
    let data = { chatType: this.data.chatType, content: this.data.content, picture : [] }
    let images = this.data.images
    images.forEach(v => {
      if(v.file !== ''){
        data.picture.push(v.file)
      }
    })
    
    if(data.chatType == 1){
      if (data.content === '' && data.picture.length == 0) {
        toast('写点东西或者上传些图片吧~')
        return
      }
      data.reward = this.data.reward
    }else{ //商品
      data.goods_name = this.data.goods_name
      if (data.goods_name === ''){
        toast('请填写产品标题~')
        return
      }
      
      if(data.content === ''){
        toast('写点东西~')
        return
      }

      if (data.picture.length == 0) {
        toast('请上传商品图片~')
        return
      }
      
      data.sale_price = this.data.sale_price
      if(data.sale_price === ''){
        toast('请填写零售价格')
        return
      }
      if (data.sale_price <= 0) {
        toast('零售价格不能小于0元')
        return
      }

      data.agent_price = this.data.agent_price
      if (data.agent_price === '') {
        toast('请填写代理价格')
        return
      }
      if (data.agent_price <= 0) {
        toast('代理价格不能小于0元')
        return
      }

      data.goods_no = this.data.goods_no
      data.add = this.data.add
      data.category_id = this.data.category_id
      if (this.data.chatId > 0) {
        data.chatId = this.data.chatId
      }

    }

    let location = this.data.location
    if (location !== ''){
      data.location = location
    }

    let lng = this.data.longitude
    let lat = this.data.latitude
    if(lng !== '' && lat !== ''){
      data.longitude = lng
      data.latitude = lat
    }

    if(this.data.chatId > 0) {
      data.chatId = this.data.chatId
    }

    request.post('publish', res => {
      if(res.success){
        toast('发布成功')
        app.newPublish = true
        wx.navigateBack({
          delta : 3
        })
      }else{
        toast(res.msg)
      }
    }, data).showLoading()

  },
  
  onLoad: function (options) {
    let barTitlle = ''
    let chatType = options.type
    let chatId = options.chatId || 0
    if (chatId == 0){ //编辑的
      if (chatType == 1) {
        barTitlle = '发布图文'
      } else {
        barTitlle = '发布产品'
      }

      wx.setNavigationBarTitle({
        title: barTitlle
      })

      this.setData({ chatType: chatType })
    }else{
      wx.setNavigationBarTitle({
        title: '编辑'
      })
      request.get('publish/' + chatId, res => {
        if(res.success) {
          let data = res.data
          let chatType = data.chat_type
          let update = {
            chatType : data.chat_type,
            content: data.content,
            location: data.location || '',
            latitude: data.latitude || '',
            longitude: data.longitude || '',

            goods_name: data.goods_name,
            goods_no: data.goods_no,
            sale_price: data.sale_price || '',
            agent_price: data.agent_price || '',
            category_id: data.category_id
          }

          if ('category_name' in data) {
            update.category_name = data.category_name
          }

          if (data.picture) {
            if (typeof data.picture == 'string') {
              data.picture = JSON.parse(data.picture)
            }
            let images = []
            data.picture.forEach((v, i) => {
              images.push({ file: v, id: i })
            })

            update.images = images
          }

          this.setData(update)
        }else{
          toast(res.msg)
        }
      })

      this.setData({ chatId: chatId})
    }


  }

})